/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.util;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 *
 */
public class SmartGwtUtil {
    public static ClickHandler getExpandClickHandler(final ListGrid grid) {
        return new ClickHandler() {
            private ListGridRecord lastExpanded = null;
            public void onClick(ClickEvent event) {
                ListGridRecord r = grid.getSelectedRecord();
                if (r != null && lastExpanded != r) {
                    grid.expandRecord(r);
                    lastExpanded = r;
                } else {
                    lastExpanded = null;
                }
            }
        };
    }

    public static Criteria getCriteria(String... values) {
        Criteria c = new Criteria();
        for (int i = 0; i+1 < values.length; i += 2) {
            c.addCriteria(values[i], values[i+1]);
        }
        return c;
    }
}
